<template>
<div :class="{'videob':type==='video','box':true}" id="editbox">
  <div v-if="type==='img'" class="imgBox">
    <img width="100%" src="https://public-server.aimeike.tv/theme/10090/thumbnail.jpg" alt="">
  </div>
  <div ref="videoBox" v-if="type==='video'" class="imgBox">
    <video
        muted
        ref="video"
        height="100%"
        @loadedmetadata="getVideo"
        src="../assets/下载.mp4"
        >
    </video>
    <div ref="line" class="line"></div>
  </div>
  <div class="imgBox textBox" v-if="type==='text'">
    <p>水电费水电费是的是的</p>
    <p>水电费水电费是的是的是的发生的</p>
  </div>
  <div class="footer">
    <div>
      {{ type==='video'?`${m}s`:'' }}
    </div>
    <div>
      <el-tooltip
          effect="dark"
          content="编辑"
          placement="top">
        <i class="el-icon-edit"></i>
      </el-tooltip>
      <el-tooltip
          effect="dark"
          content="删除"
          placement="top">
        <i class="el-icon-delete"></i>
      </el-tooltip>
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: "editBox",
  props:['type'],
  data(){
    return{
      m:0,
      ismove:true
    }
  },
  mounted() {
    if (this.type==='video'){
      this.$refs.videoBox.addEventListener('mousemove',this.moved)
      this.$refs.videoBox.addEventListener('mouseleave',this.leaved)
    }
  },
  methods:{
    moved(e){
      if (this.ismove){
        this.$refs.line.style.display = 'block'
        this.ismove = false
        let {left} = this.$refs.videoBox.getBoundingClientRect()
        let offsetWidth = this.$refs.videoBox.offsetWidth
        let pageX = e.pageX
        let lineleft = pageX - left + 2
        this.$refs.line.style.left = lineleft + 'px'
        console.log(left)
        let currentTime = (lineleft/offsetWidth)*this.m
        this.$refs.video.currentTime = Math.round(currentTime)
        console.log((lineleft/offsetWidth)*this.m)
        setTimeout(()=>{
          this.ismove = true
        },100)
      }
    },
    leaved(){
      this.$refs.line.style.display = 'none'
      this.$refs.video.currentTime = 1
    },
    getVideo(){
      this.m = this.$refs.video.duration
      this.$refs.video.currentTime = 1
    }
  }
}
</script>

<style scoped>
.footer{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px 4px;
}

.footer i{
  font-weight: bolder;
  color: #999;
  opacity: 0;
  transition: opacity 0.3s;
  cursor: pointer;
}
.footer i:nth-of-type(2){
  margin-left: 10px;
 }
.footer div:nth-of-type(1){
  color: white;
}
.footer i:hover{
  color: #2c3e50;
}
.box{
  /*width: 120px;*/
  height: 180px;
  padding: 10px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04) ;
  cursor: move;
  background-color: #fff;
  margin: 10px;
}
#editbox:hover .footer i{
  opacity: 1;
}
.box.videob{
  background-color: #CCCCCC;
}
.box.videob .footer i{
  color: white;
}
.box.videob .footer i:hover{
  color: #2c3e50;
}
.imgBox{
  width: 120px;
  height: 120px;
  background-color: #999;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.line{
  width: 1px;
  height: 120px;
  background-color: #fff;
  position: absolute;
  left: 20px;
  display: none;
}
.textBox{
  background-color: #fff;
  display: block;
}
.textBox p{
  width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.textBox p:nth-of-type(1){
  font-size: 20px;
  margin-bottom: 10px;
}
</style>